/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Finances
 *    
 *    UnitSetting
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2010-11-23
 *    @version: 1.0
 *
 ******************************************************************************/

package com.tool.finances.lifestyle;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tool.finance.manager.ShareperfrenceManager;

/**
 * UnitSetting of Finances.
 * 
 * @author zuochen
 * 
 */

public class UnitSetting extends Activity {
	private EditText edtUnit;
	private Button btnConfirm;
	private Button btnCancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unit_setting);
		bundleView();
		initData();
	}

	private void bundleView() {
		edtUnit = (EditText) findViewById(R.id.EditText01);
		btnConfirm = (Button) findViewById(R.id.btnConfirm);
		btnCancel = (Button) findViewById(R.id.btnCancel);
	}

	private void initData() {
		btnConfirm.setOnClickListener(mOnClickListener);
		btnCancel.setOnClickListener(mOnClickListener);
	}

	private final OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnConfirm:
				makeSureUnitRight();
				break;
			case R.id.btnCancel:
				finish();
				break;
			}
		}
	};

	private void makeSureUnitRight() {
		String unit = edtUnit.getText().toString();
		if (TextUtils.isEmpty(unit)
				|| unit.toString().equals("Input monetary unit")) {
			Toast.makeText(UnitSetting.this, "Unit can't be empty",
					Toast.LENGTH_SHORT).show();
		} else {
			saveUnit(unit);
			Toast.makeText(UnitSetting.this, "Unit saved successfully",
					Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	public static final String UNIT = "unit";

	private void saveUnit(String unit) {
		ShareperfrenceManager manager = new ShareperfrenceManager(
				UnitSetting.this);
		manager.saveUnit(unit);
	}

}
